<?php
header('Content-Type: text/html; charset=utf-8');
include('../../includes/classes/core.php');

	$object_id  =  $_REQUEST['id'];
	$transfer   =  $_REQUEST['transfer_id'];
	
	if($transfer[0] == 'p'){
		$page				= GetPosPage(substr($transfer,1));
		echo $page;
	}else{
		$page				= GetPage($transfer);
		echo $page;
	}


	function GetPage($transfer)
	{
		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ინფორმაცია</legend>
	
		    	<table class="dialog-form-table" border="1">
					<tr>
						<td style="width: 170px;"><label for="op_date">ოპერაციის თარიღი</label></td>
						<td>
							<span id="op_date" style="font-weight:bold; font-size:15px;">'.GetTransferDate($transfer).'</span>
						</td>
				 	</tr>
				 	
						' .GetInfo($transfer) .'
				
				</table>
				<!-- ID -->
				<input type="hidden" id="identity_id" value="'.$transfer.'" />
	        </fieldset>
    	</div>
    ';
		return $data;
	}
	
	function GetPosPage($transfer)
	{
		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ინფორმაცია</legend>
	
		    	<table class="dialog-form-table" border="1">
					<tr>
						<td style="width: 170px;"><label for="op_date">ოპერაციის თარიღი</label></td>
						<td>
							<span id="op_date" style="font-weight:bold; font-size:15px;">'.GetPosDate($transfer).'</span>
						</td>
				 	</tr>			
				    <tr>
						' .GetPosInfo($transfer) .'
					</tr>
				</table>
				<!-- ID -->
				<input type="hidden" id="identity_id" value="'.$transfer.'" />
	        </fieldset>
    	</div>
    ';
		return $data;
	}

	
	function GetInfo($transfer){
	    $data = '';
	    
	    $res1 = mysql_fetch_assoc(mysql_query("SELECT  `sale_transaction`.`sum_cost` AS `sum_cost`,
												       `sale_transaction`.`car_number` AS `car_number`,
												       `sale_transaction`.`payed_money` AS `payed_money`,
													   `persons`.`name` AS `person`,
													   ROUND(`sale_transaction`.`payed_money`-`sale_transaction`.`sum_cost`,2) AS `xurda`
												FROM `sale_transaction`
												LEFT JOIN 	`users` ON `sale_transaction`.`user_id` = `users`.`id`
												LEFT JOIN 	`persons` ON `users`.`person_id` = `persons`.`id`
												WHERE `sale_transaction`.`id` = $transfer"));
	    
	    $data .= '
	    			 <tr>
						<td style="width: 170px;"><label for="cost">შემოსავალი:</label></td>
						<td>
							<span id="cost" style="font-weight:bold; ">'.$res1['sum_cost'].' ლ</span>
						</td>
				 	 </tr>
					<tr>
						<td style="width: 170px;"><label for="cost">გადახდილი თანხა:</label></td>
						<td>
							<span id="cost" style="font-weight:bold; ">'.$res1['payed_money'].' ლ</span>
						</td>
				 	 </tr>	
					 <tr>
						<td style="width: 170px;"><label for="xurda">ხურდა:</label></td>
						<td>
							<span id="xurda" style="font-weight:bold; ">'.$res1['xurda'].' ლ</span>
						</td>
				 	 </tr>		
					 <tr>
					<td style="width: 170px;"><label for="person">მომხმარებელი:</label></td>
						<td>
							<span id="person" style="font-weight:bold; ">'.$res1['person'].'</span>
						</td>
				 	 </tr>										
					<td style="width: 170px;"><label for="car_numb">ავტ.ნომერი:</label></td>
						<td>
							<span id="car_numb" style="font-weight:bold; ">'.$res1['car_number'].'</span>
						</td>
				 	 </tr>															
	    		';
		
		$res = mysql_query("SELECT 		`sale_transaction_detail`.`id` AS `id`,
												`production`.`name` AS `name`,
												`sale_transaction_detail`.`cost` AS `cost`,
												`sale_transaction_detail`.`product_quantity` AS `quantity`,
												 ROUND(`sale_transaction_detail`.`cost` * `sale_transaction_detail`.`product_quantity`, 2 ) AS `sumCost`
									FROM 		`sale_transaction` 
									LEFT JOIN 	`sale_transaction_detail`
									ON 			`sale_transaction`.`id` = `sale_transaction_detail`.`transaction_id`
									LEFT JOIN 	`production`
									ON 			`sale_transaction_detail`.`production_id` = `production`.`id`
									WHERE   	`sale_transaction`.`id` = $transfer");
		
		$sum = mysql_fetch_assoc(mysql_query("SELECT  `sale_transaction`.`sum_cost` AS `sumCost`
												FROM  `sale_transaction`
												WHERE `sale_transaction`.`id` = $transfer"));
		
		$data .='<table style="margin-bottom:20px; width:450px;" border="1">';
		
		$data .= '<tr class="restore_list_header">
					  <td style="display:none;">&nbsp;</td>
					  <td class="restore_list_header">სახელი</td>
					  <td class="restore_list_header">რაოდ.</td>
			          <td class="restore_list_header">ერთ.ფასი</td>
					  <td class="restore_list_header">ჯ.ფასი</td>
				  </tr>';
		

		while($row = mysql_fetch_assoc($res)){
			
			$data .= '<tr>
					  <td style="display:none;">'.$row['id'].'</td>
					  <td class="restore_list"><div class="o_name">'.$row['name'].'</div></td>  
					  <td class="restore_list">'. $row['quantity'] .'</td>
					  <td class="restore_list">'.$row['cost'].'</td>
					  <td class="restore_list">'.$row['sumCost'].'</td>				  		
				  </tr>';
		}
		
		$data .= '<tr>
				<td  colspan="3">&nbsp;</td>
				<td  style="border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; text-align:center; !important; font-size:15px; background-color:tomato; padding-top:10px; padding-bottom:10px;">სულ:&nbsp;<span id="sum-price2" style="font-size:15px; font-weight:bold;">'.$sum['sumCost'] .'</span></td>
				</tr>';
		
		$data .= '</table>';
		
		return $data;
	}
	
	function GetPosInfo($transfer){
		$data ='<table style="margin-bottom:20px; width:450px;" border="1">';
		
		$res = mysql_fetch_assoc(mysql_query("	SELECT 		CONCAT('p',pos_money_action.id) AS `id`,
																pos_money_action.date AS `date`,
																IF(pos_money_action.action = 1, 'შეტანა', 'გატანა') AS `act`,
																pos_money_action.money AS `money`,
    	    													'ნაღდი' AS `pay_type`,
																persons.`name` AS `person`,
																obj1.`name` AS `object`,
																obj2.`name` AS `parent`,
																pos_money_action.`comment` AS `comment`
													FROM 		`pos_money_action`
													LEFT JOIN 	users ON pos_money_action.user_id = users.id
													LEFT JOIN 	persons ON users.person_id = persons.id
													LEFT JOIN 	object  AS `obj1` ON pos_money_action.pos_id = obj1.id
													LEFT JOIN 	object  AS `obj2` ON obj1.parent = obj2.id
												WHERE `pos_money_action`.`id` = $transfer"));
	
		$data .= '<tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">ოპერაცია</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['act'].'</td>
				  </tr>	
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">რაოდენობა</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['money'].' ლ</td>
				  </tr>						  		
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">მომხმარებელი</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['person'].'</td>
				  </tr>	
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">ობიექტი</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['object'].'</td>
				  </tr>						  		
				  <tr class="restore_list_header" style="background:none !important;">  
					  <td class="restore_list_header" style="background:none !important; color:gray;">ტიპი</td>	  
					  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['pay_type'].'</td>
				  </tr>
				  <tr>
					  <td colspan="2" style="text-align:center; padding:15px 0 5px 0;">
					  		კომენტარი
					  </td>		 
				  </tr> 						  			
				  <tr>
					  <td colspan="2">
					  		<textarea rows="4" cols="61" style="resize: none;" disabled>'.$res['comment'].'</textarea>
					  </td>		 
				  </tr> 				
				';
	
		$data .= '</table>';
	
		return $data;
	}
	

	function GetTransferDate($transfer){
		$res = mysql_fetch_assoc(mysql_query("SELECT  `sale_transaction`.`end_date` AS `end_date`
												FROM  `sale_transaction`
												WHERE `sale_transaction`.`id` = $transfer"));
		
		return $res['end_date'];
	
	}
	
	function GetPosDate($transfer){
		$res = mysql_fetch_assoc(mysql_query("SELECT   `pos_money_action`.`date` AS `date`
												FROM   `pos_money_action`
												WHERE  `pos_money_action`.`id` = $transfer"));
	
		return $res['date'];
	
	}



	